package com.fuluwa.interfaces.init.facade;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fuluwa.domain.product.model.Category;
import com.fuluwa.domain.product.model.Product;
import com.fuluwa.domain.product.repository.ProductRepository;
import com.fuluwa.domain.usercenter.model.UserClass;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.lucene.LuceneService;
import com.fuluwa.infra.lucene.LuceneServiceImpl;
import com.fuluwa.infra.web.Result;

@Service
@Transactional
public class InitService {
	
	@Autowired
	ProductRepository productRepository;

	public void doIndex() throws PersistenceException {
		List<Product> products = productRepository.findByIndexed(false);
		for (Product product : products) {
			LuceneService luceneService=new LuceneServiceImpl();
			Result result = luceneService.indexAddOrUpdadate(product.getUnquieId(), product.toSolrMap());
			if(result.isStatus()) {
				product.updateIndexed();
				product.getFileInfo().updateIndexed();
			}
		}

	}
	@Transactional
	public void doInit() throws PersistenceException {
		new UserClass("Normal").save();
		new UserClass("VIP").save();
		
		Category category1 = new Category("IT/计算机", 1, null);
		Category category11 = new Category("互联网", 2, category1);
		Category category12 = new Category("电脑基础知识", 2, category1);
		Category category13 = new Category("软件知识", 2, category1);
		category1.save();
		category11.save();
		category12.save();
		category13.save();
		Category category2 = new Category("工程科技", 1, null);
		Category category21 = new Category("通讯与信息", 2, category2);
		Category category22 = new Category("电子/电路", 2, category2);
		Category category23 = new Category("能源化工", 2, category2);
		category2.save();
		category21.save();
		category22.save();
		category23.save();
	}
}
